using Microsoft.Win32;
using NoteManger.Properties;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace NoteManger
{
	public class FrmConnectServer : TemplateForm
	{
		private IContainer components = null;
		private GroupBox groupBox;
		private PictureBox pboxServerURL;
		private Button btnCancel;
		private Button btnConnect;
		private TextBox tboxServerAddress;
		private Label label1;
		private TextBox tboxServerPort;
		private Label label2;
		private Button btnHelp;
		private NoteRemote _ObjectNoteRemote = null;
		private bool _Connected = false;
		private string _ErrorMessage = string.Empty;
		private string _ServerAddress = string.Empty;
		private string _ServerPort = string.Empty;
		private bool b_ThreadDone = true;
		public NoteRemote ObjectNoteRemote
		{
			get
			{
				return this._ObjectNoteRemote;
			}
		}
		public bool Connected
		{
			get
			{
				return this._Connected;
			}
		}
		public string ErrorMessage
		{
			get
			{
				return this._ErrorMessage;
			}
		}
		public string ServerAddress
		{
			get
			{
				return this._ServerAddress;
			}
			set
			{
				this._ServerAddress = value;
				this.tboxServerAddress.Text = this._ServerAddress;
			}
		}
		public string ServerPort
		{
			get
			{
				return this._ServerPort;
			}
			set
			{
				this._ServerPort = value;
				this.tboxServerPort.Text = this._ServerPort;
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.groupBox = new GroupBox();
			this.tboxServerPort = new TextBox();
			this.label2 = new Label();
			this.btnCancel = new Button();
			this.btnConnect = new Button();
			this.tboxServerAddress = new TextBox();
			this.label1 = new Label();
			this.pboxServerURL = new PictureBox();
			this.btnHelp = new Button();
			this.groupBox.SuspendLayout();
			((ISupportInitialize)this.pboxServerURL).BeginInit();
			base.SuspendLayout();
			this.groupBox.Controls.Add(this.btnHelp);
			this.groupBox.Controls.Add(this.tboxServerPort);
			this.groupBox.Controls.Add(this.label2);
			this.groupBox.Controls.Add(this.pboxServerURL);
			this.groupBox.Controls.Add(this.btnCancel);
			this.groupBox.Controls.Add(this.btnConnect);
			this.groupBox.Controls.Add(this.tboxServerAddress);
			this.groupBox.Controls.Add(this.label1);
			this.groupBox.Font = new System.Drawing.Font("宋体", 11f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
			this.groupBox.ForeColor = System.Drawing.Color.White;
			this.groupBox.Location = new System.Drawing.Point(12, 21);
			this.groupBox.Name = "groupBox";
			this.groupBox.Size = new System.Drawing.Size(422, 129);
			this.groupBox.TabIndex = 7;
			this.groupBox.TabStop = false;
			this.groupBox.Text = "连接远程服务器";
			this.tboxServerPort.Font = new System.Drawing.Font("宋体", 10.5f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
			this.tboxServerPort.Location = new System.Drawing.Point(347, 42);
			this.tboxServerPort.Name = "tboxServerPort";
			this.tboxServerPort.Size = new System.Drawing.Size(47, 23);
			this.tboxServerPort.TabIndex = 12;
			this.label2.AutoSize = true;
			this.label2.Font = new System.Drawing.Font("宋体", 10.5f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
			this.label2.ForeColor = System.Drawing.Color.White;
			this.label2.Location = new System.Drawing.Point(300, 46);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(49, 14);
			this.label2.TabIndex = 13;
			this.label2.Text = "端口号";
			this.btnCancel.BackColor = System.Drawing.Color.FromArgb(192, 192, 255);
			this.btnCancel.Font = new System.Drawing.Font("宋体", 10.5f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
			this.btnCancel.ForeColor = System.Drawing.Color.FromArgb(51, 79, 103);
			this.btnCancel.Location = new System.Drawing.Point(326, 91);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(70, 26);
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "关闭(&Q)";
			this.btnCancel.UseVisualStyleBackColor = false;
			this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
			this.btnConnect.BackColor = System.Drawing.Color.FromArgb(192, 192, 255);
			this.btnConnect.Font = new System.Drawing.Font("宋体", 10.5f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
			this.btnConnect.ForeColor = System.Drawing.Color.FromArgb(51, 79, 103);
			this.btnConnect.Location = new System.Drawing.Point(201, 91);
			this.btnConnect.Name = "btnConnect";
			this.btnConnect.Size = new System.Drawing.Size(119, 26);
			this.btnConnect.TabIndex = 1;
			this.btnConnect.Text = "连接服务器(&C)";
			this.btnConnect.UseVisualStyleBackColor = false;
			this.btnConnect.Click += new EventHandler(this.btnConnect_Click);
			this.tboxServerAddress.Font = new System.Drawing.Font("宋体", 10.5f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
			this.tboxServerAddress.Location = new System.Drawing.Point(142, 41);
			this.tboxServerAddress.Name = "tboxServerAddress";
			this.tboxServerAddress.Size = new System.Drawing.Size(157, 23);
			this.tboxServerAddress.TabIndex = 0;
			this.tboxServerAddress.KeyPress += new KeyPressEventHandler(this.tboxServerAddress_KeyPress);
			this.label1.AutoSize = true;
			this.label1.Font = new System.Drawing.Font("宋体", 10.5f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
			this.label1.ForeColor = System.Drawing.Color.White;
			this.label1.Location = new System.Drawing.Point(65, 45);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(77, 14);
			this.label1.TabIndex = 7;
			this.label1.Text = "服务器地址";
			this.pboxServerURL.Location = new System.Drawing.Point(12, 30);
			this.pboxServerURL.Name = "pboxServerURL";
			this.pboxServerURL.Size = new System.Drawing.Size(48, 48);
			this.pboxServerURL.SizeMode = PictureBoxSizeMode.Zoom;
			this.pboxServerURL.TabIndex = 11;
			this.pboxServerURL.TabStop = false;
			this.btnHelp.Image = Resources.information_2;
			this.btnHelp.Location = new System.Drawing.Point(12, 90);
			this.btnHelp.Name = "btnHelp";
			this.btnHelp.Size = new System.Drawing.Size(24, 24);
			this.btnHelp.TabIndex = 15;
			this.btnHelp.UseVisualStyleBackColor = true;
			this.btnHelp.Click += new EventHandler(this.btnHelp_Click);
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			this.BackColor = System.Drawing.Color.FromArgb(51, 79, 103);
			base.ClientSize = new System.Drawing.Size(446, 164);
			base.Controls.Add(this.groupBox);
			base.FormBorderStyle = FormBorderStyle.None;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "FrmConnectServer";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.groupBox.ResumeLayout(false);
			this.groupBox.PerformLayout();
			((ISupportInitialize)this.pboxServerURL).EndInit();
			base.ResumeLayout(false);
		}
		public FrmConnectServer()
		{
			this.InitializeComponent();
			try
			{
				System.Drawing.Image image = System.Drawing.Image.FromFile(NoteRemote.GetNoteClientStartPath() + "\\png\\ServerURL.png");
				this.pboxServerURL.Image = image;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}
		private void ThreadProc()
		{
			try
			{
				this._ObjectNoteRemote = (NoteRemote)Activator.GetObject(typeof(NoteRemote), string.Concat(new string[]
				{
					"http://",
					this._ServerAddress,
					":",
					this._ServerPort,
					"/NoteRemote"
				}));
			}
			catch (Exception ex)
			{
				this._ErrorMessage = ex.Message;
				this._Connected = false;
				this._ObjectNoteRemote = null;
				this.b_ThreadDone = true;
				return;
			}
			try
			{
				this._Connected = this._ObjectNoteRemote.ConnectionTest();
				if (!this._Connected)
				{
					this._ObjectNoteRemote = null;
				}
			}
			catch (Exception ex)
			{
				this._ErrorMessage = ex.Message;
				this._Connected = false;
				this._ObjectNoteRemote = null;
			}
			this.b_ThreadDone = true;
		}
		public bool ThreadConnect()
		{
			Thread thread = new Thread(new ThreadStart(this.ThreadProc));
			this.b_ThreadDone = false;
			thread.Start();
			while (!this.b_ThreadDone)
			{
				Thread.Sleep(100);
				Application.DoEvents();
			}
			return this._Connected;
		}
		public bool Connect()
		{
			bool result;
			try
			{
				this._ObjectNoteRemote = (NoteRemote)Activator.GetObject(typeof(NoteRemote), string.Concat(new string[]
				{
					"http://",
					this._ServerAddress,
					":",
					this._ServerPort,
					"/NoteRemote"
				}));
			}
			catch (Exception ex)
			{
				this._ErrorMessage = ex.Message;
				this._Connected = false;
				this._ObjectNoteRemote = null;
				result = false;
				return result;
			}
			try
			{
				this._Connected = this._ObjectNoteRemote.ConnectionTest();
				if (!this._Connected)
				{
					this._ObjectNoteRemote = null;
				}
			}
			catch (Exception ex)
			{
				this._ErrorMessage = ex.Message;
				this._Connected = false;
				this._ObjectNoteRemote = null;
				result = false;
				return result;
			}
			result = this._Connected;
			return result;
		}
		private void btnConnect_Click(object sender, EventArgs e)
		{
			this.btnCancel.Enabled = false;
			this.btnConnect.Enabled = false;
			this.btnConnect.Text = "正在连接......";
			this.btnConnect.Refresh();
			this._ServerAddress = this.tboxServerAddress.Text.Trim();
			this._ServerPort = this.tboxServerPort.Text.Trim();
			if (!this.ThreadConnect())
			{
				this.btnConnect.Text = "连接(&C)";
				this.btnConnect.Enabled = true;
				this.btnCancel.Enabled = true;
				this.btnConnect.Refresh();
				MessageBox.Show(this._ErrorMessage, "连接服务器", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
			else
			{
				RegistryKey localMachine = Registry.LocalMachine;
				RegistryKey registryKey = localMachine.OpenSubKey("Software", true);
				RegistryKey registryKey2 = registryKey.CreateSubKey("KSExpert");
				registryKey2.SetValue("NoteServerAddress", this._ServerAddress);
				registryKey2.SetValue("NoteServerPort", this._ServerPort);
				registryKey2.Close();
				registryKey.Close();
				base.DialogResult = DialogResult.OK;
			}
		}
		private void btnCancel_Click(object sender, EventArgs e)
		{
			this._Connected = false;
			this._ObjectNoteRemote = null;
			base.DialogResult = DialogResult.Cancel;
		}
		private void tboxServerAddress_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				e.Handled = true;
				this.btnConnect_Click(null, null);
			}
		}
		private void btnHelp_Click(object sender, EventArgs e)
		{
			Help.ShowHelp(new Control(), NoteRemote.GetNoteClientStartPath() + "\\日志管理系统帮助文件.chm", "连接服务器.htm");
		}
	}
}
